package com.wuye.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wuye.dao.UsersDaoImpl;
import com.wuye.entity.Users;

@Service("usersService")
public class UsersService {
	@Resource
	private UsersDaoImpl usersDao;

	public void save(Users users) {
		users.setPwd("123");
		usersDao.save(users);
	}

	public void del(String str) throws Exception {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Users users = new Users();
			users.setId(Integer.parseInt(sz[i]));
			usersDao.del(users);
		}
	}

	public void update(Users users) {
		usersDao.update(users);
	}

	public Map getList(int offset, int length) {
		String[] title = { "id", "name", "zhengjian", "dianhua", "beizhu" };
		return usersDao
				.getlist(
						title,
						"select u.id,u.name,u.zhengjian,u.dianhua,u.beizhu  from Users u where u.beizhu!='admin'",
						offset, length);
	}

	public Map getSearchList(Users users, int offset, int length)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		String[] title = { "id", "name", "zhengjian", "dianhua", "beizhu" };
		sb.append("select u.id,u.name,u.zhengjian,u.dianhua,u.beizhu  from Users u where u.beizhu!='admin'");
		if (!users.getName().equals("")) {
			sb.append(" and u.name='"
					+ new String(users.getName().getBytes("iso-8859-1"),
							"UTF-8") + "'");
			if (!users.getZhengjian().equals("")) {
				sb.append(" and u.zhengjian='" + users.getZhengjian() + "'");
			}
		} else {
			if (!users.getZhengjian().equals("")) {
				sb.append(" and u.zhengjian='" + users.getZhengjian() + "'");
			}
		}
		return usersDao.getlist(title, sb.toString(), offset, length);
	}

	public Users login(Users users) {
		String[] sz = { users.getName(), users.getPwd() };
		List list = usersDao.getUsersByHql(sz);
		if (list.size() > 0) {
			return (Users) list.get(0);
		}else{
			return null;
		}
	}

	public void rePwd(int id, String newPwd) {
		Users users = usersDao.getUsersByid(id);
		users.setPwd(newPwd);
		usersDao.update(users);
	}

	public List<Users> getComboxList() {
		return usersDao.getComboxList();
	}

	public UsersDaoImpl getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDaoImpl usersDao) {
		this.usersDao = usersDao;
	}

}
